/**
 * 
 */
package com.srini.servlets;

import java.io.IOException;
import java.util.Calendar;
import java.util.Date;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.srini.pojos.LoggedInUser;
import com.srini.pojos.PaidDayOff;
import com.srini.util.AppUtil;
import com.srini.util.ConnectionUtil;
import com.srini.util.Constants;
import com.srini.util.LeaveHolyDays;
import com.srini.util.TRMSUtil;
import com.vssr.persist.DataBaseWriter;

/**
 * @author A200171
 * 
 */
public class LeaveApplicationServlet extends HttpServlet implements Constants {

	private static final long serialVersionUID = 1L;

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		super.doGet(req, resp);
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		String reason = req.getParameter("reason");
		String from = req.getParameter("leavefrom");
		String to = req.getParameter("leaveto");
		resp.setContentType("application/html");
		String message = null;
		PaidDayOff off = new PaidDayOff();
		LoggedInUser user = (LoggedInUser) req.getSession().getAttribute(
				LOGGEDIN_USER);

		Date fromDate = null, toDate = null;
		try {
			fromDate = DATE_FORMAT_MEDIUM.parse(from);
			toDate = DATE_FORMAT_MEDIUM.parse(to);
		} catch (Exception e) {
			e.printStackTrace();
		}
		Calendar startCal;
		Calendar endCal;
		startCal = Calendar.getInstance();
		startCal.setTime(fromDate);
		endCal = Calendar.getInstance();
		endCal.setTime(toDate);
		long diff = AppUtil.daysBetween(startCal, endCal) + 1;

		Map days = LeaveHolyDays.getHolidays(user.getId(),
				new ConnectionUtil().getConnection());
		int day, dayCount = 0;
		String date = null;
		for (int i = 0; i < diff; i++) {
			date = DATE_FORMAT_MEDIUM.format(startCal.getTime());
			day = TRMSUtil.checkDayOfWeek(days, startCal);
			if (day == 0) {
				off = new PaidDayOff();
				off.setDayOffType(LEAVE);
				off.setPaidDayOff(date);
				off.setUserId(user.getId());
				DataBaseWriter.save(off);
				dayCount++;
			}
			startCal.add(Calendar.DATE, 1);
		}
		if (dayCount > 0) {
			String noOfDays = dayCount + (dayCount > 1 ? " days" : " day");
			message = "Leave successfully applied for " + noOfDays;
			System.out.println("Hi " + user.getManagerId() + ", \n"
					+ user.getUserName() + " has applied leave from " + from
					+ " to " + to + "(" + noOfDays + ") for " + reason);
		} else {
			message = "Error::Leave might have already applied on this day or its a holiday";
		}
		String currentPage = "leavepage";
		req.setAttribute("message", message);
		req.setAttribute("currentPage", currentPage);
		req.getRequestDispatcher("/home.jsp").forward(req, resp);
		// resp.sendRedirect("main.jsp");
	}
}
